package com.example.springstack.springsecurity.service.impl;


import com.exampl.springstack.common.GlobalConst;
import com.exampl.springstack.common.utils.BeanHelper;
import com.exampl.springstack.common.utils.LocalCacheUtils;
import com.example.springstack.springsecurity.entity.User;
import com.example.springstack.springsecurity.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Qiangfanghao
 */
@Service("userService")
public class UserServiceImpl implements UserService, UserDetailsService {

    @Autowired
    private PasswordEncoder pw;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        if (!username.equals("admin")){
            throw new  UsernameNotFoundException("用户名密码错误");
        }
        User user = new User();

        String encodePass = pw.encode("admin");

        user.setUsername("admin");
        user.setId(1);
        user.setPassword(encodePass);
        user.setNickname("test");
        user.setEnabled(true);
        user.setLocked(false);

        return user;
    }

    @Override
    public void insertUser(User user) {  //对密码进行加密
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodePass = encoder.encode(user.getPassword());
        user.setPassword(encodePass);
        user.setUserStateId(2);
        user.setEnabled(true);
        user.setLocked(false);
    }

    @Override
    public List<GrantedAuthority> getUserAuthorities(String username) {
        //获取本地缓存
        Object obj = LocalCacheUtils.get(username + GlobalConst.AUTHORITIED_KEY);
        //获取本地缓存
//        List<SysOperation> userOperation = BeanHelper.objToList(obj, SysOperation.class);
        //使用 List进行演示 将角色拥有的权限查询出来
        List<String> userOperation = new ArrayList<>();
        userOperation.add("TEST");
        userOperation.add("admin");
        userOperation.add("ROLE_admin");
        //如果为空 这进行查询
        if (CollectionUtils.isEmpty(userOperation)){
//            userOperation = this.baseMapper.getUserOperation(username);
//            存入缓存 缓存时间 30分钟
            LocalCacheUtils.push(username+ GlobalConst.AUTHORITIED_KEY,userOperation,30 * 60);
        }

        //添加权限
        ArrayList<GrantedAuthority> grantedAuthorities = new ArrayList<>();

        userOperation.forEach((sysOperation)->{
            grantedAuthorities.add(new SimpleGrantedAuthority(sysOperation)) ;
        });
        return grantedAuthorities;
    }
}
